package cn.com.isfree.db.config;

import java.util.Properties;

import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.tio.client.AioClient;
import org.tio.client.ClientChannelContext;
import org.tio.client.ClientGroupContext;
import org.tio.client.ReconnConf;
import org.tio.client.intf.ClientAioHandler;
import org.tio.client.intf.ClientAioListener;
import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.core.Node;
import org.tio.utils.json.Json;

import cn.com.isfree.db.config.bean.MapBean;
import cn.com.isfree.db.config.bean.PropertiesBean;
import cn.com.isfree.db.config.bean.TioBean;
import cn.com.isfree.tio.client.ShowcaseClientAioHandler;
import cn.com.isfree.tio.client.ShowcaseClientAioListener;
import cn.com.isfree.tio.common.ShowcasePacket;
import cn.com.isfree.tio.common.ShowcaseSessionContext;
import cn.com.isfree.tio.common.Type;
import cn.com.isfree.tio.common.packets.LoginReqBody;

@Configuration
public class InitConfig {
	
	public static ClientChannelContext clientChannelContext;
	
	public static StdSchedulerFactory factory;
	
	//public static Scheduler scheduler;
	
	private static Logger log = LoggerFactory.getLogger(InitConfig.class);
	
	@Autowired
	private TioBean tioBean;
	
	@Autowired
	private PropertiesBean propertiesBean;
	
	@Bean
	public ClientChannelContext initClientGroupContext() {
		System.out.println("------------->初始化tio客户端连接");
		try {
			Node serverNode = new Node(tioBean.getServer().getIp(), tioBean.getServer().getPort());
			// 用来自动连接的，不想自动连接请设为null
			ReconnConf reconnConf = new ReconnConf(tioBean.getClient().getReconnConf());
			ClientAioHandler aioClientHandler = new ShowcaseClientAioHandler();
			ClientAioListener aioListener = new ShowcaseClientAioListener() {
				@Override
				public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect) throws Exception {
					log.info("onAfterConnected channelContext:{}, isConnected:{}, isReconnect:{}", channelContext, isConnected, isReconnect);
					if(isReconnect) {
						LoginReqBody loginReqBody = new LoginReqBody();
						loginReqBody.setLoginname(tioBean.getClient().getLoginname());
						loginReqBody.setPassword(tioBean.getClient().getPassword());
						ShowcasePacket reqPacket = new ShowcasePacket();
						reqPacket.setType(Type.LOGIN_REQ);
						reqPacket.setBody(Json.toJson(loginReqBody).getBytes(tioBean.getClient().getShowcasePacket().getCharset()));
						if(Aio.send(InitConfig.clientChannelContext, reqPacket)) {
							System.out.println("------------->tio客户端重登录成功:"+tioBean.getClient().getLoginname());
						}else{
							System.out.println("------------->tio客户端重登录失败:"+tioBean.getClient().getLoginname());
						}
					}
					//连接后，需要把连接会话对象设置给channelContext
					channelContext.setAttribute(new ShowcaseSessionContext());
				}
			};
			ClientGroupContext clientGroupContext = new ClientGroupContext(aioClientHandler, aioListener, reconnConf);
			AioClient aioClient = new AioClient(clientGroupContext);
			InitConfig.clientChannelContext = aioClient.connect(serverNode);
			//登录到web端
			LoginReqBody loginReqBody = new LoginReqBody();
			loginReqBody.setLoginname(tioBean.getClient().getLoginname());
			loginReqBody.setPassword(tioBean.getClient().getPassword());
			ShowcasePacket reqPacket = new ShowcasePacket();
			reqPacket.setType(Type.LOGIN_REQ);
			reqPacket.setBody(Json.toJson(loginReqBody).getBytes(tioBean.getClient().getShowcasePacket().getCharset()));
			if(Aio.send(InitConfig.clientChannelContext, reqPacket)) {
				System.out.println("------------->tio客户端登录成功:"+tioBean.getClient().getLoginname());
			}else{
				System.out.println("------------->tio客户端登录失败:"+tioBean.getClient().getLoginname());
			}
			return InitConfig.clientChannelContext;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@Bean
	public StdSchedulerFactory initScheduler() {
		System.out.println("------------->初始化quartz任务");
		System.out.println(Json.toJson(propertiesBean));
		try {
			if(propertiesBean.getPropertiesMap()!=null) {
				Properties props = new Properties();
				for(MapBean map : propertiesBean.getPropertiesMap()) {
					props.put(map.getKey(), map.getValue());
				}
				InitConfig.factory = new StdSchedulerFactory();
				//初始化
				InitConfig.factory.initialize(props);
				//初始化
				InitConfig.factory.getScheduler();
				return InitConfig.factory;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
